package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.kaishengit.entity.MyFile;
import com.kaishengit.entity.Files;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.Page;
import com.kaishengit.util.RowMapper;

public class FileDao {
	private DBHelp<MyFile> db = new DBHelp<MyFile>();

	public Page<MyFile> findAllFIleByFilesId(String id,Page<MyFile> page) {
		StringBuilder sql =new StringBuilder();
		sql.append("SELECT t.id,`name`,filename,size,t.t_user_id,t.t_files_id,tu.username,tu.password,tu.imgname ,tfs.f_name FROM t_file AS t  ");
		sql.append("LEFT JOIN t_user AS tu  ON tu.id = t.t_user_id ");
		sql.append("LEFT JOIN t_files AS tfs ON tfs.id = t.t_files_id ");
		sql.append("WHERE t.t_files_id  =? LIMIT ?,?");
		List<MyFile> list = db.executeQueryForList(sql.toString(), new FileRowMapper(), id,page.getOffest(),page.getPageSize());
		page.setList(list);
		return page;	
	}
	public class FileRowMapper2 implements RowMapper<MyFile>{

		public MyFile mapRow(ResultSet rs) throws SQLException {
			MyFile file = new MyFile();
			file.setFileName(rs.getString("filename"));
			file.setId(rs.getString("id"));
			file.setName(rs.getString("name"));
			file.setSize(rs.getInt("size"));
			file.setFilesize(rs.getInt("size"));
			file.setUserId(rs.getString("t_user_id"));
			file.setFilesId(rs.getString("t_files_id"));
			String name = file.getName();
			file.setType(name.substring(name.lastIndexOf(".")));
			return file;
		}
		
	}
	private class FileRowMapper implements RowMapper<MyFile>{

		public MyFile mapRow(ResultSet rs) throws SQLException {
			MyFile file = new MyFile();
			file.setFileName(rs.getString("filename"));
			file.setId(rs.getString("id"));
			file.setName(rs.getString("name"));
			file.setSize(rs.getInt("size"));
			file.setFilesize(rs.getInt("size"));
			file.setUserId(rs.getString("t_user_id"));
			file.setFilesId(rs.getString("t_files_id"));
			String name = file.getName();
			file.setType(name.substring(name.lastIndexOf(".")));
			
			User user = new User();
			user.setId(rs.getString("t_user_id"));
			user.setImgname(rs.getString("imgname"));
			user.setPassword(rs.getString("password"));
			user.setUsername(rs.getString("username"));
			file.setUser(user);
			
			Files files = new Files();
			files.setId(rs.getString("t_files_id"));
			files.setFilesname(rs.getString("f_name"));
			file.setFiles(files);
			
			
			return file;
		}
		
	}
	public MyFile findFileByTrueName(String trueName) {
		String sql = "select id,name,filename,size,t_user_id,t_files_id from t_file where filename = ?";
		return db.executeQueryForObject(sql, new FileRowMapper2(), trueName);
	}
	public boolean save(MyFile file) {
		String sql = "insert into t_file (id,name,filename,size,t_user_id,t_files_id) values(?,?,?,?,?,?)";
		 return db.executeSQL(sql, file.getId(),file.getName(),file.getFileName(),file.getSize(),file.getUserId(),file.getFilesId());
		
	}
	public void del(String id) {
		String sql = "delete from t_file where id = ?";
		db.executeSQL(sql, id);
	}
	public void delByFilesId(String id) {
		
		
	}
	public List<MyFile> findallFileByFilesId(String id) {
		String sql = "select id,name,filename,size,t_user_id,t_files_id from t_file where t_files_id = ?";
		return db.executeQueryForList(sql, new FileRowMapper2(), id);
	}
	
}
